$(function () {
    $("#jqGrid").jqGrid({
        datatype:'local',
        colModel: [
            { label: '交易id', name: 'transactionId', index: "transactionId", width: 45, key: true,align:'center' , sortable: false },
            { label: '交易金额', name: 'amount', width: 75,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: 'SN', name: 'sn', width: 100,align:'center' , sortable: false},
            { label: '终端名称', name: 'terminalName', width: 50,align:'center' , sortable: false},
            { label: '终端型号', name: 'model', width: 50,align:'center' , sortable: false},
            { label: '商户号', name: 'merchantCode', width: 100,align:'center' , sortable: false},
            { label: '交易时间', name: 'transactionDate', width:75,align:'center' , sortable: false},
            { label: '是否vip', name: 'isVip', width: 75,align:'center' , sortable: false},
            { label: '是否分润', name: 'isBenefited', width: 75,align:'center' ,formatter:function(value,options,row){
                    return value==0?"否":"是";
                } , sortable: false},

			{ label: '是否线上', name: 'isOnline', width: 75,align:'center',formatter:function(value,options,row){
                    return value==0?"非线上":"线上";
                } , sortable: false},
            { label: '是否激活', name: 'isActivation', width: 75,align:'center',formatter:function(value,options,row){
                    return value==1?"未激活":"已激活";
                },sortable:false},
            { label: '交易人姓名', name: 'name', width: 75,align:'center',sortable: false},
            { label: '交易人手机', name: 'phone', width: 75,align:'center',sortable: false},

        ],
		viewrecords: true,
        height: 420,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 50,
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",

        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    }).setGridParam();

    bindHeadType();
});


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            startDate:null,
            endDate:null,
            name:null,
            agentId:null,
            type:0,
            terminalType:0,
            isBenefited:-1

        },
        terminalTypes:[],
        showList: true,
        title:null,
        agentList:[],

    },
    methods: {
        query: function () {
            if(vm.q.name=='' || vm.q.name==null){
                layer.msg("请输入姓名");
                return;
            }
            $.ajax({
                url:baseURL+"sys/sysAgent/byName",
                data:{"name":vm.q.name},
                dataType:"json",
                type:"post",
                success:function (r) {
                    if(r.list.length==1){
                        vm.q.agentId=r.list[0].agentId;
                        vm.seachAmount();
                    }else if (r.list.length>1) {
                        openDialog(r);
                    }else if(r.list.length<1){
                        layer.msg("查无此人,请核实姓名")
                    }
                }

            })
        },
        searchOne:function(obj){
            var aEl=$(obj.srcElement);
            vm.q.agentId=$(aEl).parent().find("input").val();
            layer.close(agentDialog);
            vm.seachAmount();
        },
        queryAll:function(){
            vm.q.type=0;
            $("#jqGrid").jqGrid('setGridParam',{'page':1});
            vm.reload();
        },
        queryOwner:function(){
            vm.q.type=2;
            $("#jqGrid").jqGrid('setGridParam',{'page':1});
            vm.reload();
        },
        queryTeam:function(){
            vm.q.type=1;
            $("#jqGrid").jqGrid('setGridParam',{'page':1});
            vm.reload();
        },
        seachAmount:function(){
            vm.q.startDate=$("#startDate").val();
            vm.q.endDate=$("#endDate").val();
            $.ajax({
                url:baseURL+'sys/transactionInfo/amountDetail',
                data:JSON.stringify(vm.q),
                dataType:"json",
                contentType:"application/json",
                type:"post",
                success:function (r) {
                    $("#total").html(r.result.totalAmount);
                    $("#owner").html(r.result.ownerAmount);
                    $("#team").html(r.result.teamAmount);
                    $("#showAmount").show();
                    $("#jqGrid").jqGrid('clearGridData')
                }
            })
        },
        clean:function(){
            vm.showList = true;
            vm.q={
                startDate:null,
                endDate:null,
                name:null,
                agentId:null,
                type:0,
                terminalType:0,
                isBenefited: -1
            };
            $("#endDate").val("");
            $("#startDate").val("");
            $("#jqGrid").jqGrid('clearGridData');
            $("#showAmount").hide();

        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            vm.q.startDate=$("#startDate").val();
            vm.q.endDate=$("#endDate").val();
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'startDate': vm.q.startDate,'endDate':vm.q.endDate,'agentId':vm.q.agentId,"type":vm.q.type,"terminalType":vm.q.terminalType,'isBenefited':vm.q.isBenefited},
                datatype:'json',
                url: baseURL + 'sys/transactionInfo/detail',
                page:page,
            }).trigger("reloadGrid");
        }
    }
});
var agentDialog;
function openDialog(r){
    for (var i = 0; i < r.list.length; i++) {
        vm.$set(vm.agentList, i, r.list[i]);
    }
    agentDialog=layer.open({
        type:1,
        title:"请选择",
        content:$("#agentList"),
        area:['400px','280px'],
        btn:['确定'],
        btn1:function (index) {
            layer.close(index);
        }
    })
}

function bindHeadType() {
        $.getJSON(baseURL + "sys/terminalType/list", function (r) {
            vm.$set(vm.terminalTypes, 0, {
                terminalType: 0,
                terminalName: "请选择类型"
            });
            for (var i = 0; i < r.page.length; i++) {
                vm.$set(vm.terminalTypes, (i + 1), r.page[i]);
            }
            vm.q.terminalType = 0;
        })

}